package com.sumainfor.schedule.service.impl;

import com.sumainfor.common.Constants;
import com.sumainfor.common.alipayutils.AlipayUtil;
import com.sumainfor.common.entity.TemplateData;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.CompuUtils;
import com.sumainfor.common.utlis.Jsapi_ticketUtil;
import com.sumainfor.common.wechat.WeChatConfig;
import com.sumainfor.schedule.feign.ZpcGmioFeign;
import com.sumainfor.schedule.feign.ZpcTenantFeign;
import com.sumainfor.schedule.util.BaseService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.MapUtils;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zc
 * @ClassName TenBillService
 * @date 2019/12/29 11:50
 * @description: TODO
 */
@Log4j2
@Service
public class TenBillService extends BaseService {

    @Autowired
    ZpcGmioFeign zpcGmioFeign;

    @Autowired
    ZpcTenantFeign zpcTenantFeign;

    @Autowired
    BaseService baseService;



    public void TenBillCon(Map<String,Object> params){
        this.setHeadType();
        params.put("BiType",1);//账单类型房租
        params.put("BiTypeTime",1);//账单类型房租
        log.info("222222222");
        log.info("params"+params);
        JsonResult result1 = zpcGmioFeign.getSchedulBill(params);
       List<Map<String,Object>> BillList = (List<Map<String, Object>>) result1.get("result");//得到房租账单
        log.info("BillList"+BillList);
       if (BillList.size()>0&&BillList!=null){
           for (Map<String,Object> BillMap:BillList){
               //根据房租账单里面来源的合同编号,租客编号房源编号,获取有没有固定金额的水电燃账单
               double billMoneySum = MapUtils.getDouble(BillMap,"BiMoney");

               Map<String,Object> mapOne =new HashMap<>();
               mapOne.put("TenId",BillMap.get("TenId"));
               mapOne.put("BiType",2);
               mapOne.put("Status",1);
               mapOne.put("BiTypeTime",1);
               mapOne.put("HouId",BillMap.get("HouId"));
               mapOne.put("HouInfoId",BillMap.get("HouInfoId"));
               mapOne.put("Source",BillMap.get("Source"));
               JsonResult result2 = zpcGmioFeign.getSchedulBill(mapOne);
               List<Map<String,Object>> BillListOne =(List<Map<String, Object>>) result2.get("result");
               if (BillListOne.size()>0&&BillListOne!=null){//加上固定的公共事业费
                   for (Map<String,Object> mapOnes :BillListOne){
                       billMoneySum = CompuUtils.add(billMoneySum,MapUtils.getDouble(mapOnes,"BiMoney"));
                   }
               }

               JsonResult result3 = zpcTenantFeign.getTenOpenId(BillMap);

               Map<String,Object> ceshiMap = (Map<String, Object>) result3.get("result");//获取租客openid
           if (MapUtils.getInteger(BillMap,"DiffBillDay")==15||MapUtils.getInteger(BillMap,"DiffBillDay")==3){//账单在15天和三天的时候会推送微信给租客
               //推送微信模板
               String touser=ceshiMap.get("uopid").toString();//租客的openid
               Map<String, TemplateData> param = new HashMap<>();
               param.put("first", new TemplateData("唐巢公寓温馨提示：尊敬的租客，您有待支付的账单。请您尽快支付。如有异议，请联系您的租赁顾问人员为您服务。\n", "#4876FF"));
               param.put("keyword1", new TemplateData(BillMap.get("BiGenerated").toString()+"-"+BillMap.get("EndTime"), "#4876FF"));//账单周期
               param.put("keyword2", new TemplateData(String.valueOf(billMoneySum), "#4876FF"));//账单金额
               param.put("keyword3", new TemplateData(BillMap.get("BiGenerated").toString(), "#4876FF"));//最晚缴费日期
               log.info("param:"+net.sf.json.JSONObject.fromObject(param));
               Jsapi_ticketUtil.sendTemplate(touser, WeChatConfig.FOOMWEG_ID,"http://www.4006269069.net/#/allBill","#4876FF", net.sf.json.JSONObject.fromObject(param));
           }
           if (MapUtils.getInteger(BillMap,"DiffBillDay")==3){//在三天的时候还会推送短信模板给租客

           }


           }




       }







    }
}
